<?php
//Data loading
//By Bayu
$pdf=new COAPDFReport("P","mm","Letter");
$pdf->DBConnection($DBConnection->dbc);
$pdf->Period(GetParam("y",""),GetParam("m",""),GetParam("ly",""),GetParam("lm",""));
$pdf->AddPage();
$pdf->GenerateReport();
$pdf->Output();

class COAPDFReport extends PDF {
  function COAPDFReport($Page,$Size,$PageMode) {
    parent::PDF($Page,$Size,$PageMode);
    $this->ColumnSize = array(85,30,30,30,30);
    $this->TableName = "tblcoa";
    $this->AddFont('arialn','','arialn.php');
    $this->SetFont('arialn','',10);
    $this->SetLeftMargin(5);
  }

  function Header() {
      $title = "INCOME STATEMENT";
      $page   = 'HAL';
      $title .= (GetParam("m","")=="13")?' (AUDIT)':"";
      $perush = (GetParam("office",""))?GetCompany (GetParam("office","")):'PT. AMPEL JAYA';

        $this->SetFont('arialn','',10);
      $this->Cell(200,5,$perush,0,1,'C',0);
      $this->Cell(200,5,$title,0,1,'C',0);
      $Periode = (GetParam("m","")=="13")?$this->BuildPeriod (GetParam("y",""), 12):$this->BuildPeriod (GetParam("y",""), GetParam("m",""));
      $this->Cell(200,5,"PERIODE: ".$Periode,0,1,'C',0);
      $this->Cell(200,5,$page." ".$this->PageNo(),0,1,'R',0);

        //Column widths
        $this->SetFont('arialn','',10);
        $this->Cell($this->ColumnSize[0],7,"DESKRIPSI",1,0,'C',0);
        $this->Cell($this->ColumnSize[1],7,"S/D BLN LALU",1,0,'C',0);
        $this->Cell($this->ColumnSize[2],7,"MUTASI",1,0,'C',0);
        $this->Cell($this->ColumnSize[3],7,"S/D BLN INI",1,0,'C',0);
        $this->Cell($this->ColumnSize[4],7,"BLN KE-".GetParam("lm","")." THN ".GetParam("ly",""),1,0,'C',0);
        $this->Ln();
        $this->SetFont('arialn','',8);
  }

  function BuildPeriod ($year, $month) {
    $arraymonth = array("","JANUARI","FEBRUARI","MARET","APRIL","MEI","JUNI","JULI","AGUSTUS","SEPTEMBER","OKTOBER","NOPEMBER","DESEMBER");
    return strtoupper(date("d ", mktime(0, 0, 0, $month+1, 0, $year))).$arraymonth[$month].strtoupper(date(" Y", mktime(0, 0, 0, $month+1, 0, $year)));
  }

  function Footer() {
    $this->Cell(205,0,'','T');
  }

  function Period($Year, $Month, $LastYear, $LastMonth){
    if ($Month<10) $Month = "0".$Month;
    if (($Month-1)<10) {$LMonth="0".($Month-1);}
    else $LMonth=$Month-1;
    if ($LastMonth<10) $LastMonth = "0".$LastMonth;
    $this->_CYear  = $Year;
    $this->_CMonth = $Month;
    $this->_SMonth = "aku".$Month;
    $this->_LastYear = $LastYear;
    $this->_LastMonth = "aku".$LastMonth;
    if ($Month==1) $this->_LSMonth = "awal";
    else $this->_LSMonth = "aku".$LMonth;
  }

  function DBConnection ($Conn) {
    $this->DBConnection = $Conn;
  }

  function GenerateReport() {
    $SQL = "SELECT * FROM rpt_inc_stmt";
    $MyResults = $this->DBConnection->get_results($SQL,ARRAY_A);

    foreach ($MyResults as $RowResults){
      $this->BlankLine();

      if ($RowResults['istotal']=="1") {
        $SkadaRow = explode(',',$RowResults['skada']);
        //print_r($SkadaRow);
        for($x=0;$x<count($SkadaRow);$x++) {
          $SubTotalLast = $SubTotalLast + $this->SubTotal[$SkadaRow[$x]]["lastmonth"];
          $SubTotalMutasi = $SubTotalMutasi + $this->SubTotal[$SkadaRow[$x]]["mutasi"];
          $SubTotalCurr = $SubTotalCurr + $this->SubTotal[$SkadaRow[$x]]["currmonth"];
          $SubTotalLastYear = $SubTotalLastYear + $this->SubTotal[$SkadaRow[$x]]["awal"];
          //print "$SubTotalLast $SubTotalMutasi $SubTotalCurr $SubTotalLastYear"."<p>\n";
        }
        if ($RowResults['inverse']==1) {
          $SubTotalLast = $SubTotalLast *-1;
          $SubTotalMutasi = $SubTotalMutasi *-1;
          $SubTotalCurr = $SubTotalCurr *-1;
          $SubTotalLastYear = $SubTotalLastYear *-1;
        }

          $this->WriteLine("   ".$RowResults['deskripsi'],FormatNumber($SubTotalLast),FormatNumber($SubTotalMutasi),FormatNumber($SubTotalCurr),FormatNumber($SubTotalLastYear));
          $SubTotalLast = 0;
          $SubTotalMutasi = 0;
          $SubTotalCurr = 0;
          $SubTotalLastYear = 0;

      } else {
        $this->WriteLine($RowResults['deskripsi'],"","","","");
        $this->BlankLine();
        //print "{$RowResults['deskripsi']}<p>\n";
          if (ereg(",",$RowResults['skada'])) {
            $SkadaRow = explode(',',$RowResults['skada']);
            for($x=0;$x<count($SkadaRow);$x++) {
              $TotalRow = $this->WriteRow($SkadaRow[$x],$RowResults['inverse']);
              $TotalLast = $TotalLast + ($RowResults['inverse']==1)?$TotalRow['lastmonth']*-1:$TotalRow['lastmonth'];
              $TotalMutasi = $TotalMutasi + ($RowResults['inverse']==1)?$TotalRow['mutasi']*-1:$TotalRow['mutasi'];
              $TotalCurr = $TotalCurr + ($RowResults['inverse']==1)?$TotalRow['currmonth']*-1:$TotalRow['currmonth'];
              $TotalLastYear = $TotalLastYear + ($RowResults['inverse']==1)?$TotalRow['awal']*-1:$TotalRow['awal'];
            }
          } else {
              $TotalRow = $this->WriteRow($RowResults['skada'],$RowResults['inverse']);
              $TotalLast = ($RowResults['inverse']==1)?$TotalRow['lastmonth']*-1:$TotalRow['lastmonth'];
              $TotalMutasi = ($RowResults['inverse']==1)?$TotalRow['mutasi']*-1:$TotalRow['mutasi'];
              $TotalCurr = ($RowResults['inverse']==1)?$TotalRow['currmonth']*-1:$TotalRow['currmonth'];
              $TotalLastYear = ($RowResults['inverse']==1)?$TotalRow['awal']*-1:$TotalRow['awal'];
          }
              $this->BlankLine();
              $this->WriteLine("* * * TOTAL ".$RowResults['deskripsi']." * * *",FormatNumber($TotalLast),FormatNumber($TotalMutasi),FormatNumber($TotalCurr),FormatNumber($TotalLastYear));
              $this->SubTotal[$RowResults['skada']]["lastmonth"] = $TotalRow['lastmonth'];
              $this->SubTotal[$RowResults['skada']]["mutasi"] = $TotalRow['mutasi'];
              $this->SubTotal[$RowResults['skada']]["currmonth"] = $TotalRow['currmonth'];
              $this->SubTotal[$RowResults['skada']]["awal"] = $TotalRow['awal'];
      }
    }
  }

  function WriteLine($Description, $sdbl, $mutasi, $sdblini, $sdlastyear, $isblock="0") {
    if ($isblock=="1") $border = "1"; else $border = "LR";
    $this->Cell($this->ColumnSize[0],5,$Description,$border,0,'L',0);
    $this->Cell($this->ColumnSize[1],5,$sdbl,$border,0,'R',0);
    $this->Cell($this->ColumnSize[2],5,$mutasi,$border,0,'R',0);
    $this->Cell($this->ColumnSize[3],5,$sdblini,$border,0,'R',0);
    $this->Cell($this->ColumnSize[4],5,$sdlastyear,$border,0,'R',0);
    $this->Ln();
  }

  function BlankLine() {
    $this->WriteLine("","","","","");
  }

  function GetTotalRow($parent_skada) {
    $SQL = "

    SELECT a.skada, b.{$this->_LastMonth} as awal,a.{$this->_SMonth} as currmonth,a.{$this->_LSMonth} as lastmonth
    FROM {$this->TableName} a
    LEFT JOIN {$this->TableName} b ON a.skada=b.skada AND b.ledger={$this->_LastYear}
    WHERE a.ledger='{$this->_CYear}' and a.parent='$parent_skada'

    ";
    $MyResults = $this->DBConnection->get_results($SQL,ARRAY_A);
    if ($MyResults) {
       foreach ($MyResults as $RowResults){
          $ResultTotalRow    = $this->GetTotalRow($RowResults['skada']);
          $Rows['currmonth'] = $Rows['currmonth']+ $ResultTotalRow['currmonth'] + $RowResults['currmonth'];
          $Rows['lastmonth'] = $Rows['lastmonth'] + $ResultTotalRow['lastmonth'] + $RowResults['lastmonth'];
          $Rows['awal']      = $Rows['awal'] + $ResultTotalRow['awal'] + $RowResults['awal'];
          $Rows['mutasi']    = $Rows['currmonth'] - $Rows['lastmonth'];
       }
    }
    return $Rows;
  }

  function WriteRow($parent_skada,$inverse) {
     $SQL = "SELECT namacc,skada FROM {$this->TableName} WHERE ledger='{$this->_CYear}' and parent='$parent_skada' ";
     //die($SQL);
     $MyResults = $this->DBConnection->get_results($SQL,ARRAY_A);
     if ($MyResults) {
       foreach ($MyResults as $RowResults){
         $TotalRow = $this->GetTotalRow($RowResults['skada']);
         //print  "{$RowResults['namacc']},{$TotalRow['lastmonth']},{$TotalRow['mutasi']},{$TotalRow['currmonth']},$Last_Year";
         if ($inverse==1) {
            $this->WriteLine("   ".$RowResults['namacc'],FormatNumber($TotalRow['lastmonth']*-1),FormatNumber($TotalRow['mutasi']*-1),FormatNumber($TotalRow['currmonth']*-1),FormatNumber($TotalRow['awal']*-1));
         } else
            $this->WriteLine("   ".$RowResults['namacc'],FormatNumber($TotalRow['lastmonth']),FormatNumber($TotalRow['mutasi']),FormatNumber($TotalRow['currmonth']),FormatNumber($TotalRow['awal']));

        $TotalLast = $TotalLast + $TotalRow['lastmonth'];
        $TotalMutasi = $TotalMutasi + $TotalRow['mutasi'];
        $TotalCurr = $TotalCurr + $TotalRow['currmonth'];
        $TotalLastYear = $TotalLastYear + $TotalRow['awal'];
       }
       $TotalAccount['lastmonth'] = $TotalLast;
       $TotalAccount['mutasi'] = $TotalMutasi;
       $TotalAccount['currmonth'] = $TotalCurr;
       $TotalAccount['awal'] = $TotalLastYear;
       return $TotalAccount;
     }
  }

}
?>
